import cookie from 'cookie_js';

const defaultOptions = {
  domain: window.location.hostname.endsWith('dip-aitech.com')
    ? 'dip-aitech.com'
    : window.location.hostname,
  path: '/',
};

const getCookie = (keys, defaultValue) => {
  if (typeof keys === 'string') {
    return cookie.get(keys, defaultValue);
  }
  if (Array.isArray(keys)) {
    if (keys.length === 0) {
      return cookie.all();
    }
    return cookie.get(keys, defaultValue);
  }
  return null;
};

const setCookie = (params = {}, options) => {
  cookie.set(params, {
    ...defaultOptions,
    ...options,
  });
};

const removeCookie = (keys, options) => {
  if (Array.isArray(keys) && keys.length === 0) {
    return cookie.empty();
  }
  return cookie.removeSpecific([].concat(keys), {
    ...defaultOptions,
    ...options,
  });
};

export { getCookie, setCookie, removeCookie };
